package com.xmy.cultivate.service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.entity.CourseSchedulingDetail;
import com.xmy.cultivate.entity.RecordCourse;
import com.xmy.cultivate.entity.SchoolQuarter;
import com.xmy.cultivate.entity.views.AiSitePcLoginRs;
import com.xmy.cultivate.feign.english.IFeiEnglishFeignClient;
import com.xmy.cultivate.mapper.AiSchoolSiteMapper;
import com.xmy.cultivate.respons.AiSchoolSite;
import com.xmy.cultivate.service.IAiSchoolSiteService;
import com.xmy.cultivate.service.ICourseSchedulingDetailService;
import com.xmy.cultivate.service.IMqPushService;
import com.xmy.cultivate.service.IRecordCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  校区场地表 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-28
 */
@Service
public class AiSchoolSiteServiceImpl extends ServiceImpl<AiSchoolSiteMapper, AiSchoolSite> implements IAiSchoolSiteService {
    @Autowired
    IFeiEnglishFeignClient iFeiEnglishFeignClient;

    @Autowired
    IRecordCourseService iRecordCourseService;

    @Autowired
    ICourseSchedulingDetailService iCourseSchedulingDetailService;

    @Autowired
    IMqPushService iMqPushService;

    @Override
    public List<SchoolQuarter> aiSchoolSiteList(String yearPart, String quarterNum, String schoolId) {

        return iFeiEnglishFeignClient.aiSchoolSiteList(yearPart, quarterNum, schoolId);
    }

    @Override
    public boolean siginAllotPc(Long courseSchedulingDetailId, Long studentId, String studentName, Integer signType, String schoolSiteId, Long siginInTeacherId, LocalDateTime kickOutTime){
        //签到
        try {
            LocalDateTime nowDateTime = LocalDateTime.now().withNano(0);
            if (signType.equals(1) && StringUtils.isNotEmpty(schoolSiteId)) {
                RecordCourse recordCourse = iRecordCourseService.getOneForSchedulingDetailId(courseSchedulingDetailId,studentId);
                if (recordCourse!=null){
                    //签到智能课，推送到分配电脑
                    //if (recordCourse.getLessonType().equals(2) && recordCourse.getSubjectsId().equals(1L)){
                    if (recordCourse.getLessonType().equals(2) || recordCourse.getLessonType().equals(3)){
                        AiSitePcLoginRs aiSitePcLoginRs = new AiSitePcLoginRs();
                        LocalDateTime startDateTime = LocalDateTime.now().withNano(0);
                        LocalDateTime endDateTime = startDateTime.plusHours(1);

                        //后面是否有排课还未到时间
                        List<CourseSchedulingDetail> courseSchedulingDetailBeforList = iCourseSchedulingDetailService.getBeforSchedulingOne(courseSchedulingDetailId,recordCourse.getSchoolId(),studentId,recordCourse.getSubjectsId(),recordCourse.getLessonType(), LocalDate.now().toString(),startDateTime.toString(),endDateTime.toString());
                        CourseSchedulingDetail courseSchedulingDetailBefor = null;
                        Integer i = 0;
                        Integer siginAllotType = null;
                        Long delaySecond = 0L;
                        for (CourseSchedulingDetail courseSchedulingDetail : courseSchedulingDetailBeforList) {
                            if (i.equals(0)){
                                courseSchedulingDetailBefor = courseSchedulingDetail;
                            }
                            if (courseSchedulingDetail.getId().equals(courseSchedulingDetailId)){
                                Duration duration = Duration.between(nowDateTime,courseSchedulingDetail.getStartDateTime());
                                Long secondsStart = duration.getSeconds();//上课相差秒
                                if (secondsStart>0){
                                    siginAllotType = 3;
                                    delaySecond = secondsStart;
                                }
                            }
                            i++;
                        }
                        if (courseSchedulingDetailBefor != null){
                            Duration duration = Duration.between(nowDateTime,courseSchedulingDetailBefor.getStartDateTime());
                            Long secondsStart = duration.getSeconds();//上课相差秒
                            aiSitePcLoginRs.setNextSchoolTime(courseSchedulingDetailBefor.getStartDateTime());
                            aiSitePcLoginRs.setNextSchedulintDetailId(courseSchedulingDetailBefor.getId());
                            aiSitePcLoginRs.setUnderCount(courseSchedulingDetailBefor.getUnderCount());
                            aiSitePcLoginRs.setKickOutTime(nowDateTime);//允许踢除时间
                            //负数为迟到
                            aiSitePcLoginRs.setDelaySecondAllot(Integer.parseInt(secondsStart.toString()));
                            if (courseSchedulingDetailBefor.getId().equals(courseSchedulingDetailId)){
                                if (secondsStart>0){
                                    aiSitePcLoginRs.setSiginAllotType(3);//在排课时间内提前到，有电脑直接分电脑，没有电脑时排号等到上课时间到，开始踢除最早上机的学生分配电脑
                                }else {
                                    aiSitePcLoginRs.setSiginAllotType(1);//迟到，有电脑就直接分配
                                }
                                aiSitePcLoginRs.setSchedulingType(1);//当前时间段内属于自己的排班
                            }else {
                                if (secondsStart<=0){
                                    aiSitePcLoginRs.setSiginAllotType(1);//直接分电脑
                                    aiSitePcLoginRs.setSchedulingType(2);//当前时间有其它的排班
                                }else {
                                    aiSitePcLoginRs.setSiginAllotType(2);//如下一个班还未满班，先分完未满人数，再排队，等上课的时间到了再分电脑
                                    aiSitePcLoginRs.setSchedulingType(0);//1个小时内有其它班排班
                                }
                            }
                        }else {
                            aiSitePcLoginRs.setSiginAllotType(1);//直接分电脑
                        }

                        if (siginAllotType!=null){
                            aiSitePcLoginRs.setSiginAllotType(siginAllotType);
                            aiSitePcLoginRs.setDelaySecondAllot(Integer.parseInt(delaySecond.toString()));
                        }

                        //aiSitePcLoginRs.setSchoolId();
                        aiSitePcLoginRs.setSchoolSiteId(Long.parseLong(schoolSiteId));
                        aiSitePcLoginRs.setSubjectsId(recordCourse.getSubjectsId());
                        aiSitePcLoginRs.setYearPart(recordCourse.getYearPart());
                        aiSitePcLoginRs.setQuarterNum(recordCourse.getQuarter());
                        aiSitePcLoginRs.setStudentId(studentId);
                        aiSitePcLoginRs.setStudentName(studentName);
                        aiSitePcLoginRs.setUserId(siginInTeacherId);
                        aiSitePcLoginRs.setSchedulingDetailId(courseSchedulingDetailId);
                        iMqPushService.allotPc(aiSitePcLoginRs);
                    }
                }
            }
        }catch (Exception e){}
        return true;
    }
}
